package learning.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UDP2 {// recieve port
	public static void main(String[] args) {
		try {
			DatagramSocket s = new DatagramSocket(8888);
			new Rec_msg(s).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

class Rec_msg extends Thread {
	DatagramSocket s;

	public Rec_msg(DatagramSocket s) {
		super();
		this.s = s;
	}

	@Override
	public void run() {
		SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
		byte[] buf = new byte[1024 * 64];
		DatagramPacket packet = new DatagramPacket(buf, buf.length);
		System.out.println("waiting.....");
		try {
			while (true) {
				s.receive(packet);
				System.out.println("start recieveing.....");
				System.out.println("dialogue:"+new String(packet.getData())+"\tsource port:"+packet.getPort()+"\t"+sdf.format(new Date()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
